package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R addAddress(@RequestBody AddressBook addressBook, HttpSession session){
        //获取当前登录用户
        User user = (User) session.getAttribute("user");
        Long userId = user.getId();

        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R setDefaultAddr(@RequestBody AddressBook addressBook,HttpSession session){
        log.info("设置默认地址");
        //获取当前用户信息
        User user = (User) session.getAttribute("user");
        //获取用户id
        Long userId = user.getId();
        //先将当前用户所有地址的is_default设置为0
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.set(AddressBook::getIsDefault,0).eq(AddressBook::getUserId,userId);
        addressBookService.update(luw);
        //获取地址id
        Long addrId = addressBook.getId();
        //设置为默认地址
        AddressBook addrBook = addressBookService.getById(addrId);
        addrBook.setIsDefault(1);
        addressBookService.updateById(addrBook);

        return R.success("设置成功");
    }

    /**
     * 查询默认地址
     * @param session
     * @return
     */
    @GetMapping("/default")
    public R getDefAddr(HttpSession session){
        //获取当前登录用户
        User user = (User) session.getAttribute("user");
        //获取当前登录用户id
        Long userId = user.getId();

        //根据用户id查询默认地址
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(lqw);

        return R.success(addressBook);
    }

    /**
     * 查询指定用户的所有地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R getAddres(HttpSession session){
        //获取当前登录用户信息
        User user = (User) session.getAttribute("user");
        //根据用户id查询该用户所有地址
        Long id = user.getId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,id);
        lqw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delAddres(@RequestParam List<Long> ids){

        addressBookService.removeByIds(ids);

        return R.success("删除成功");
    }

    /**
     * 根据地址id查询地址信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getAddrById(@PathVariable Long id){

        AddressBook address = addressBookService.getById(id);

        return R.success(address);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R updateAddr(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }
}
